﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.DomainModel;
using Enterprise.Framework.Core.Exceptions;
using Enterprise.Framework.DomainModel.Specifications;
using Enterprise.Framework.DomainModel.Repositories;
using Resto.CostControl.Core.Domain.Resource;
using Resto.CostControl.Common.Utility;

namespace Resto.CostControl.Core.Domain.Model
{
    /// <summary>
    /// 菜肴
    /// </summary>
    public partial class Dish : IAggregationRoot
    {

        #region 构造函数

        internal Dish()
        {
            this.ID = IDHelper.CreateNewID();
        }

        #endregion

        #region 私有属性

        private IEntityRepository<Dish> _Repository;
        private IEntityRepository<Dish> Repository
        {
            get
            {
                if (_Repository == null)
                    _Repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<Dish>();

                return _Repository;
            }
        }

        #endregion

        #region 业务方法

        internal void Add()
        {
            Validate();

            ReComputeCost();

            this.CreateAudit = new Audit();
            this.LastUpdateAudit = new Audit();

            this.Repository.Add(this);
        }

        internal void Update()
        {
            Validate();

            ReComputeCost();

            this.LastUpdateAudit = new Audit();

            this.Repository.Update(this);
        }

        internal  void Delete()
        {
            this.IsDelete = true;

            this.LastUpdateAudit = new Audit();

            this.Repository.Update(this);
        }

        internal void AddMaterial(DishMaterial material)
        {
            //判断品名是否已经在列表中
            bool result = this.DishMaterials.Where(p => p.MaterialID == material.MaterialID).Count() > 0;
            if (result)
                throw new BusinessValidationException(DishResource.ValidateException_DishMaterial_Repeat);

            this.DishMaterials.Add(material);
        }

        //internal void RemoveMaterial(DishMaterial material)
        //{
        //    this.DishMaterials.Remove(material);
        //}

        internal void ClearMaterial()
        {
            DishMaterial[] array = this.DishMaterials.ToArray();
            foreach (DishMaterial item in array)
                item.Delete();

            this.DishMaterials.Clear();
        }

        /// <summary>
        /// 根据菜单配比重新计算成本
        /// </summary>
        private void ReComputeCost()
        { 
            //todo:
        }

        #endregion

        #region 验证方法

        protected void Validate()
        {
            List<string> messageList = new List<string>();

            ValidateNameRepeat(messageList);
            ValidateCodeRepeat(messageList);

            if (messageList.Count > 0)
                throw new BusinessValidationException(messageList.ToArray());
        }

        private void ValidateNameRepeat(IList<string> messageList)
        {
            ISpecification<Dish> spec = Specification<Dish>.Create(p => p.Name == this.Name);
            spec = spec.And(Specification<Dish>.Create(p => p.ID != ID));

            bool result = Repository.Count(spec) > 0;
            if (result)
                messageList.Add(string.Format(DishResource.ValidateException_DishName_Repeat, this.Name));
        }

        private void ValidateCodeRepeat(IList<string> messageList)
        {
            ISpecification<Dish> spec = Specification<Dish>.Create(p => p.Code == this.Code);
            spec = spec.And(Specification<Dish>.Create(p => p.ID != ID));

            bool result = Repository.Count(spec) > 0;
            if (result)
                messageList.Add(string.Format(DishResource.ValidateException_DishCode_Repeat, this.Code));
        }

        #endregion
    }
}
